/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Cvars.h"
#include <stdlib.h>
#include <sstream>

#include "Kernel.h"

extern Kernel* g_kernel;

Cvars::Cvars(void)
{

}

/**
======================
Initialize cvars 
======================
*/
void Cvars::Init()
{
    //
    // Video properties
    RegisterCvar("v_height", "600", 600.0f, CVAR_SAVE);
    RegisterCvar("v_width", "800", 800.0f, CVAR_SAVE);
    RegisterCvar("v_render", "opengl", 0.0f, CVAR_SAVE);
    RegisterCvar("v_fullscreen", "0", 0.0f, CVAR_SAVE);
    RegisterCvar("v_maxfps", "30", 30.0f, CVAR_SAVE);
    RegisterCvar("v_debug", "0", 0.0f, CVAR_SAVE );
    
    //
    // Sound properties
    RegisterCvar("s_sound", "1", 1.0f, CVAR_SAVE);
    RegisterCvar("s_volume", "0.8", 0.8f, CVAR_SAVE);
    RegisterCvar("s_mvolume", "0.8", 0.8f, CVAR_SAVE);

    //
    // Util properties
    RegisterCvar("logger", "1", 1.0f, CVAR_SAVE);
    RegisterCvar("cl_fps" , "1", 1.0f, CVAR_SAVE);
    RegisterCvar("debug", "1", 1.0f, CVAR_SAVE);
    RegisterCvar("startscript", "startup.lua", 0.0f, CVAR_SAVE|CVAR_PROTECT );

    //
    // Input properties
    RegisterCvar("i_joystick", "1", 1.0f, CVAR_SAVE);
    RegisterCvar("i_registerInput", "1", 1.0f, CVAR_SAVE );

    //
    // Game properties
    RegisterCvar("g_menu_script", "menu_init.lua", 0.0f, CVAR_SAVE|CVAR_PROTECT );

}

/** Get a cvar float value */
float Cvars::GetFloatValue( const std::string &cvar_name )
{
    if ( cvarList.find( cvar_name ) != cvarList.end() )
    {
        return cvarList[cvar_name].fvalue;
    }
    return 0.0;
}

/** Get a string value */
std::string Cvars::GetStringValue( const std::string &cvar_name )
{
    if ( cvarList.find( cvar_name ) != cvarList.end() )
    {
        return cvarList[cvar_name].svalue;
    }
    return "";
}

/** Create a cvar */
bool Cvars::RegisterCvar( const std::string &cvar_name, const std::string &sval, float val, int Flags )
{
    cvar c;
    c.flags     = Flags;
    c.fvalue    = val;
    c.name      = cvar_name;
    c.svalue    = sval;

    cvarList[cvar_name] = c;
    return true;
}

/** Remove a cvar */
void Cvars::RemoveCvar( const std::string &cvar_name )
{
    if ( cvarList.find( cvar_name ) != cvarList.end() )
    {
        cvarList.erase( cvar_name );
    }
}

/** Set a cvar value */
bool Cvars::SetCvarValue( const std::string &name, float val )
{
    // check if the cvar exists
    if ( cvarList.find( name ) != cvarList.end() )
    {
        // make sure it is not write protected
        if ( cvarList[name].flags & CVAR_PROTECT )
        {
            return false;
        }
        // fill in the values
        cvarList[name].fvalue = val;
        // convert a float to a string
        std::stringstream buf;
        buf << val;
        cvarList[name].svalue = buf.str();
        return true;
    }
    return false;
}

/** Set a cvar value */
bool Cvars::SetCvarValue(const std::string &name, const std::string &value)
{
    // check if the cvar exists
    if ( cvarList.find( name ) != cvarList.end() )
    {
        // make sure it is not write protected
        if ( cvarList[name].flags & CVAR_PROTECT )
        {
            return false;
        }
        // fill in the values
        cvarList[name].fvalue = (float)atof(value.c_str());
        cvarList[name].svalue = value;
        return true;
    }
    return false;
}

/**
==========================
Check to see if this is a valid cvar
==========================
*/
bool Cvars::IsValid(const std::string &name)
{
    return cvarList.find( name ) != cvarList.end();
}


Cvars::~Cvars(void)
{
//    g_kernel->LPrintf("Closing Cvars...");
}
